package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import JDBC.DatabaseDao;
import Utils.Utils;

public class UserRelationDao {
	static Connection con = DatabaseDao.getConnection();

	/**
	 * @author 韩 闻文 total relations
	 * @return
	 */
	public static int getCount() {
		int result = 0;
		try {
			String sql = "select count(*) from user_relation";
//			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * @author 韩 闻文
	 * compute total two-direction relations
	 * @return
	 */
	public static String getReciprocityCount() {
		String result = "";
		try {
			String sql = "select count(*) from user_relation as T1,user_relation as T2 where T1.source_user_id=T2.target_user_id and T1.target_user_id=T2.source_user_id";
			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result+=rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * @author 韩 闻文 compute two-direction relations of a certain user
	 * @param userId
	 * @return
	 */
	public static int getReciprocityCountByUserId(String userId) {
		int result = 0;
		try {
			String sql = "select count(*) from user_relation as T1,user_relation as T2 where T1.source_user_id=T2.target_user_id and T1.target_user_id=T2.source_user_id and T1.source_user_id=" + userId;
			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result = rs.getInt(1);
			System.out.println("reciprocity:" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String[] getFollowersByUserId(String userId) {
		ArrayList<String> followers = new ArrayList<String>();
		try {
			String sql = "select source_user_id from user_relation where target_user_id =" + userId;
			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String follower_id = rs.getString("source_user_id");
				followers.add(follower_id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Utils.arrayList2Array(followers);
	}

	public static String [] getFriendsByUserId(String userId) {
		ArrayList<String> friends = new ArrayList<String>();
		try {
			String sql = "select target_user_id from user_relation where source_user_id =" + userId;
			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String friend_id = rs.getString("target_user_id");
				friends.add(friend_id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Utils.arrayList2Array(friends);
	}
	
	public static ArrayList<String> getAllFollowers() {
		ArrayList<String> followers = new ArrayList<String>();
		try {
			String sql = "select source_user_id from user_relation";
//			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String follower_id = rs.getString("source_user_id");
				followers.add(follower_id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return followers;
	}
	public static ArrayList<String> getAllFriendss() {
		ArrayList<String> friends = new ArrayList<String>();
		try {
			String sql = "select target_user_id from user_relation";
//			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String friend_id = rs.getString("target_user_id");
				friends.add(friend_id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return friends;
	}

}
